package com.example.wei;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.example.wei.view.PullRefreshView;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    private PullRefreshView mainPullRefreshView;

    private ArrayList<CommentBenan.ProductsDTO> arrayList = new ArrayList<>();
    private CommentAdapter adapter = new CommentAdapter(arrayList);
    private int cursor = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i("TAGS", "MyActivity");

        mainPullRefreshView = findViewById(R.id.mainPullRefreshView);

        //设置适配器跟列表绑定
        mainPullRefreshView.setLoad();//设置列表正在加载数据
        //线性布局管理器
        //mainPullRefreshView.setLayoutManager(new LinearLayoutManager(this));
        //网格布局管理器
        // mainPullRefreshView.setLayoutManager(new GridLayoutManager(this,3));
        //瀑布流管理器
        mainPullRefreshView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        mainPullRefreshView.setAdapter(adapter);

        mainPullRefreshView.setOnRefreshListener(new PullRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新回调
                getData();
            }

            @Override
            public void onLoadMore() {
                //上拉加载回调
                getData();
            }
        });

        adapter.setOnItemClickListener(new CommentAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, CommentBenan.ProductsDTO dto) {
                ToastUtil.show(MainActivity.this,position+"|"+dto.getPrice());
            }

            @Override
            public void onLongClick(int position, CommentBenan.ProductsDTO dto) {
                ToastUtil.show(MainActivity.this,position+"|"+dto.getPrice());
            }
        });

        getData();

    }

    private void getData() {

        OkGo.<String>get("http://svsign.yokey.top/douyin/store/product/list?" +
                "appId=1596128648987&appKey=534e40448ee86160acdbdc3948ac903&secShopId=IKZFCSqO&cursor=" + cursor)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        CommentBenan bean = new Gson().fromJson(response.body(), CommentBenan.class);
                        //数据正常返回的请求
                        if (bean.getProducts().size() != 0) {
                            //正常返回数据
                            arrayList.addAll(bean.getProducts());
                            mainPullRefreshView.setComplete();
                            cursor += 20;//修改 cursor的值 确保能够读取下一页数据
                        } else {
                            if (cursor == 0) {
                                mainPullRefreshView.setEmpty();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (cursor == 0) {
                            mainPullRefreshView.setError(response.getException().getLocalizedMessage());
                        } else {
                            ToastUtil.show(MainActivity.this, response.getException().getLocalizedMessage());
                        }
                    }
                });

    }

    //评论数据转换
    //数据的刷新
    //数据的加载下一页

}

